package com.spzx.product.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.service.ICategoryBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.Arrays;
import java.util.List;

@Tag(name = "分类品牌")
@RestController
@RequestMapping("/categoryBrand")
public class CategoryBrandController extends BaseController {
    @Autowired
    private ICategoryBrandService categoryBrandService;


    @Operation(summary = "分页列表")
    @GetMapping("/list")
    public TableDataInfo getCategoryBrandList(CategoryBrand categoryBrand) {
        startPage();
        List<CategoryBrand> categoryBrandList = categoryBrandService.selectCategoryBrandList(categoryBrand);
        return getDataTable(categoryBrandList);
    }

    @Operation(summary = "获取分类品牌详细信息")
    @GetMapping("/{id}")
    public AjaxResult getCategoryBrandInfo(@PathVariable("id") Long id) {
        return success(categoryBrandService.getCategoryBrandInfo(id));
    }

    @Operation(summary = "新增分类品牌")
    @PostMapping
    public  AjaxResult addCategoryBrand(@RequestBody @Validated CategoryBrand categoryBrand){
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        return  toAjax(categoryBrandService.insertCategoryBrand(categoryBrand));
    }

    @Operation(summary = "修改分类品牌")
    @PutMapping
    public AjaxResult updateCategoryBrand(@RequestBody @Validated CategoryBrand categoryBrand){
        categoryBrand.setUpdateBy(SecurityUtils.getUsername());
        return  toAjax(categoryBrandService.updateCategoryBrand(categoryBrand));
    }

    @Operation(summary = "删除分类品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Long[ ] ids){
        return toAjax(categoryBrandService.removeBatchByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "根据分类id获取品牌列表")
    @GetMapping("brandList/{categoryId}")
    public  AjaxResult selectBrandListByCategoryId(@PathVariable Long categoryId){
        return success(categoryBrandService.getBrandListByCategoryId(categoryId));
    }
}
